/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.generator.entityGenerator.entity.elements.EntityElementEnumGenerator;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CastExpression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;

public class SimpleUdtDataValuePropGenerator extends ClassPropertyGenerator {

  private SimpleDataTypeDef sdef;

  public SimpleUdtDataValuePropGenerator(SimpleDataTypeDef sdef) {
    this.sdef = sdef;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getGetterAccessModifier() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<Modifier.ModifierKeyword>();
    list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getSetterAccessModifier() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<Modifier.ModifierKeyword>();
    list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
    return list;
  }

  @Override
  protected String getPropertyName() {
    return "Value";
  }

  private TypeInfo info;
  @Override
  protected void beforeGenerate() {
    info =getSimpleUdtDataGenericType(sdef);
    info.setUseFullName(true);
    super.beforeGenerate();
  }

  public static TypeInfo getSimpleUdtDataGenericType(SimpleDataTypeDef sdef) {
    IGspCommonField element = sdef.getContainElements().get(0);
    TypeInfo info;
    switch (sdef.getObjectType()) {
      case None:
        info = Utils.getNativeType(element, null);
        break;
      case Enum:
        info = new TypeInfo(EntityElementEnumGenerator.getName(element)) {{
          setTypePackageName(sdef.getGeneratedEntityClassInfo().getClassNamespace());
          setUseFullName(true);
        }};
        break;
      case Association:
        info = new TypeInfo(element.getLabelID() + "Info") {{
          setTypePackageName(sdef.getGeneratedEntityClassInfo().getClassNamespace());
          setUseFullName(true);
        }};
        break;
      default:
        throw new RuntimeException();
    }
    return info;
  }

  @Override
  protected TypeInfo getPropertyType() {
    return info;
  }

  @Override
  protected String getFieldName() {
    return "Value";
  }


  @Override
  protected ArrayList<AnnotationInfo> getGetterAttributeList() {
    java.util.ArrayList<AnnotationInfo> list = super.getGetterAttributeList();
    if (list == null) {
      list = new java.util.ArrayList<AnnotationInfo>();
    }
    AnnotationInfo annotationInfo = new AnnotationInfo();
    annotationInfo.setAnnotationType(AnnotationType.SingleMember);
    annotationInfo.setTypeInfo(new TypeInfo(JsonIgnore.class));
    annotationInfo.setSingleParam(ast.newBooleanLiteral(true));
    list.add(annotationInfo);
    return list;
  }


  @Override
  protected Block buildGetMethodBody() {
    Block block = ast.newBlock();
    ReturnStatement returnStatement = ast.newReturnStatement();
    MethodInvocation invocation = ast.newMethodInvocation();
    invocation.setName(ast.newSimpleName(buildGetterMethodName()));
    returnStatement.setExpression(invocation);
    block.statements().add(returnStatement);
    return block;
  }

  @Override
  protected Block buildSetMethodBody() {
    Block block = ast.newBlock();
    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setName(ast.newSimpleName(buildSetterMethodName()));

    CastExpression castExpression = ast.newCastExpression();
    castExpression.setType(info.getType(ast));
//        ast.newSimpleType(ast.newName(valueType.getPropertyMethod().getReturnType().getTypeName())));
    castExpression.setExpression(ast.newSimpleName("value"));
    methodInvocation.arguments().add(castExpression);

    block.statements().add(ast.newExpressionStatement(methodInvocation));
    return block;
  }

  private String buildGetterMethodName(){
    return "get".concat(sdef.getContainElements().get(0).getLabelID());
  }

  private String buildSetterMethodName(){
    return "set".concat(sdef.getContainElements().get(0).getLabelID());
  }
}
